<?php

namespace app\admin\controller;

use app\common\model\Article;

use support\Request;

class DashboardController extends BaseController
{
    /**
     * Index
     */
    public function index(Request $request)
    {
        // 当天开始时间
        $todayBegin = strtotime(date('Y-m-d', time()));
        // 当天结束之间
        $todayEnd = $todayBegin + 60*60*24;
        // 本月开始时间
        $monthBegin = mktime(0, 0, 0, date('m'), 1, date('Y'));
        // 本月结束之间
        $monthEnd = mktime(23, 59, 59, date('m'), date('t'), date('Y'));

        // 总文章
        $totalArticle = Article::where('status', '<>', 90)->count();
        // 本月文章
        $monthArticle = Article::where('status', '<>', 90)->where('created_at', '>=', $monthBegin)->where('created_at', '<=', $monthEnd)->count();
        //$monthArticle = Article::where('status', '<>', 90)->whereMonth('created_at', date('m'))->count();
        // 今日文章
        $todayArticle = Article::where('status', '<>', 90)->where('created_at', '>=', $todayBegin)->where('created_at', '<=', $todayEnd)->count();
        //$todayArticle = Article::where('status', '<>', 90)->whereDay('created_at', date('d'))->count();
        // 总会员
        $totalUser = 200000;
        // 本月会员
        $monthUser = mt_rand(1000, 3000);
        // 今日会员
        $todayUser = mt_rand(100, 500);

        $data = [
            'totalOrder' => $totalArticle,
            'orderAmount' => $monthArticle,
            'totalGoods' => $todayArticle,
            'monthUser' => $monthUser,
            'totalUser' => $totalUser,
        ];

        return view('dashboard/index', [
            'data' => $data
        ]);
    }

    /**
     * 订单统计
     */
    public function orderCharts(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->all();
            $where = [
                ['status', '<>', 90]
            ];
            $statistics = [
                'day' => ['2022-11', '2022-12', '2023-01', '2023-02'],
                'count' => [mt_rand(20, 100), mt_rand(20, 100), mt_rand(20, 100), mt_rand(20, 100)],
                'totalAmount' => [mt_rand(800, 4000), mt_rand(800, 4000), mt_rand(800, 4000), mt_rand(800, 4000)],
            ];

            return json([
                'code' => 1,
                'data' => $statistics
            ]);
        }
    }
}
